package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.bo.ParkLeaveQueryPriceVo;
import com.ruoyi.system.domain.ZsyyParkingCarLog;
import com.ruoyi.system.dto.ParkLeavePriceVo;
import com.ruoyi.system.vo.GangTingLeaveVo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyParkLeaveAddBo;
import com.ruoyi.system.bo.ZsyyParkLeaveQueryBo;
import com.ruoyi.system.bo.ZsyyParkLeaveEditBo;
import com.ruoyi.system.domain.ZsyyParkLeave;
import com.ruoyi.system.mapper.ZsyyParkLeaveMapper;
import com.ruoyi.system.vo.ZsyyParkLeaveVo;
import com.ruoyi.system.service.IZsyyParkLeaveService;
import com.ruoyi.system.mapper.ZsyyParkLeaveMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 车辆离场记录Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkLeaveServiceImpl extends ServicePlusImpl<ZsyyParkLeaveMapper, ZsyyParkLeave> implements IZsyyParkLeaveService {
    @Autowired
    private ZsyyParkLeaveMapper iZsyyParkLeaveMapper;

    @Override
    public ZsyyParkLeaveVo queryById(Long id){
        return getVoById(id, ZsyyParkLeaveVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkLeaveVo> queryPageList(ZsyyParkLeaveQueryBo bo) {
		Page<ZsyyParkLeaveVo> page = iZsyyParkLeaveMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }
    @Override
    public TableDataInfo<ZsyyParkLeaveVo> leaveRecordList(ZsyyParkLeaveQueryBo bo) {
		Page<ZsyyParkLeaveVo> page = iZsyyParkLeaveMapper.leaveRecordList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

	@Override
	public TableDataInfo<GangTingLeaveVo> gangtingList(ZsyyParkLeaveQueryBo bo) {
		Page<GangTingLeaveVo> page = iZsyyParkLeaveMapper.gangtingList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
	}

	@Override
	public List<GangTingLeaveVo> exportGangTing(ZsyyParkLeaveQueryBo bo) {
		return iZsyyParkLeaveMapper.exportGangTing(bo);
	}

	@Override
	public List<ZsyyParkLeaveVo> exportLeaveRecord(ZsyyParkLeaveQueryBo bo) {
		return iZsyyParkLeaveMapper.exportLeaveRecord(bo);
	}

	@Override
    public List<ZsyyParkLeaveVo> queryList(ZsyyParkLeaveQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyParkLeaveVo.class);
    }

    private LambdaQueryWrapper<ZsyyParkLeave> buildQueryWrapper(ZsyyParkLeaveQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyParkLeave> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getCarLicenseNumber()), ZsyyParkLeave::getCarLicenseNumber, bo.getCarLicenseNumber());
        lqw.eq(bo.getType() != null, ZsyyParkLeave::getType, bo.getType());
		lqw.between(StrUtil.isNotBlank(bo.getLeaveTime()), ZsyyParkLeave::getLeaveTime,bo.getLeaveTime(),bo.getEndLeaveTime());
		lqw.orderByDesc(ZsyyParkLeave::getLeaveTime);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkLeaveAddBo bo) {
        ZsyyParkLeave add = BeanUtil.toBean(bo, ZsyyParkLeave.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkLeaveEditBo bo) {
        ZsyyParkLeave update = BeanUtil.toBean(bo, ZsyyParkLeave.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyParkLeave entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public ParkLeavePriceVo getPriceTotal(ParkLeaveQueryPriceVo priceVo) {
		return iZsyyParkLeaveMapper.getPriceTotal(priceVo);
	}


}
